package icu.sehnen.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import icu.sehnen.news.entity.MyPage;
import icu.sehnen.news.entity.News;
import icu.sehnen.news.mapper.NewsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import icu.sehnen.news.service.INewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author icu.sehnen
 * @since 2021-04-26
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

@Autowired
NewsMapper newsMapper;
    @Override
    public News findNewsById(Long id) {
      return   newsMapper.findNewsById(id);
    }

    @Override
    public IPage<News> findAll(Integer pageNum,Integer pageSize, News news) {
//        QueryWrapper<News> queryWrapper = new QueryWrapper();
//        queryWrapper.like(!StringUtils.isBlank(news.getTitle()),"title",news.getTitle());
//        queryWrapper.between(news.getCreateTime()!=null,"create_time",news.getCreateTime(),getNow());
       return  newsMapper.findAll(new MyPage<News>(pageNum,pageSize),news);
    }
    private Date getNow() {
        Date date = new Date();
        SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format0.format(date);
        Date time1= null;
        try {
            time1 = format0.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time1;
    }
}
